package com.dongfanggroup.report.service.impl;

import cn.hutool.core.date.DateUtil;
import com.dongfanggroup.report.domain.ReportOrder;
import com.dongfanggroup.report.domain.ReportType;
import com.dongfanggroup.report.domain.req.ReportOrderReq;
import com.dongfanggroup.report.enums.ReportOrderStatusEnum;
import com.dongfanggroup.report.mapper.ReportOrderMapper;
import com.dongfanggroup.report.service.IReportOrderHistoryService;
import com.dongfanggroup.report.service.IReportOrderService;
import com.dongfanggroup.report.service.IReportTypeService;
import com.github.jaemon.dinger.DingerSender;
import com.github.jaemon.dinger.core.entity.DingerRequest;
import com.github.jaemon.dinger.core.entity.enums.MessageSubType;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 报障单Service业务层处理
 *
 * @author wangsr
 * @date 2021-12-16
 */
@Service
public class ReportOrderServiceImpl implements IReportOrderService {
    @Autowired
    private ReportOrderMapper reportOrderMapper;

    @Autowired
    private IReportTypeService reportTypeService;

    @Autowired
    private IReportOrderHistoryService reportOrderHistoryService;

    @Autowired
    private DingerSender dingerSender;

    /**
     * 查询报障单
     *
     * @param id 报障单主键
     * @return 报障单
     */
    @Override
    public ReportOrder selectReportOrderById(Long id) {
        return reportOrderMapper.selectReportOrderById(id);
    }

    /**
     * 查询报障单列表
     *
     * @param reportOrder 报障单
     * @return 报障单
     */
    @Override
    @DataScope(userAlias = "ro")
    public List<ReportOrder> selectReportOrderList(ReportOrderReq reportOrder) {
        return reportOrderMapper.selectReportOrderList(reportOrder);
    }

    /**
     * 新增报障单
     *
     * @param reportOrder 报障单
     * @return 结果
     */
    @Override
    public int insertReportOrder(ReportOrder reportOrder) {
        reportOrder.setReportUserId(SecurityUtils.getUserId());
        Long reportTypeBigStr = reportOrder.getReportTypeBig();
        Long reportTypeSmallStr = reportOrder.getReportTypeSmall();
        ReportType reportTypeBig = reportTypeService.selectReportTypeById(reportTypeBigStr);
        ReportType reportTypeSmall = reportTypeService.selectReportTypeById(reportTypeSmallStr);
        reportOrder.setReportTypeBigName(reportTypeBig.getName());
        reportOrder.setReportTypeSmallName(reportTypeSmall.getName());
        reportOrder.setReportStatus(ReportOrderStatusEnum.CREATE.getCode());
        reportOrder.setReportSubmitTime(DateUtils.getNowDate());
        reportOrder.setReportDealCount(1);

        reportOrder.setCreateBy(SecurityUtils.getUserId() + "");
        reportOrder.setCreateTime(DateUtils.getNowDate());
        reportOrder.setUserId(SecurityUtils.getUserId());
        reportOrder.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId());

        reportOrder.setReportFiles(String.join(";", reportOrder.getUploadFiles()));
        ReportOrder dbOrder = new ReportOrder();
        while (dbOrder != null) {
            //生成id
            String reportId = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
            //先查询
            dbOrder = reportOrderMapper.selectReportOrderByReportId(reportId);
            if (dbOrder == null) {
                reportOrder.setReportId(Long.valueOf(reportId));
            }
        }
        int i = reportOrderMapper.insertReportOrder(reportOrder);
        reportOrderHistoryService.insertNewLog(reportOrder.getId(), "新增报障单", ReportOrderStatusEnum.CREATE);

        DingerRequest request = DingerRequest.request("# 新增报障单 \n" +
                String.format("- 提交人: %s\n", SecurityUtils.getLoginUser().getSysUser().getNickName()) +
                String.format("- 问题大类: %s\n", reportTypeBig.getName()) +
                String.format("- 问题小类: %s\n", reportTypeSmall.getName()) +
                String.format("- 问题标题: %s\n", reportOrder.getReportTitle()) +
                String.format("- 问题描述: %s\n", reportOrder.getReportDesc())
        );
        request.setTitle("报障单通知");
        dingerSender.send(
                MessageSubType.MARKDOWN,
                request
        );
        return i;
    }


    /**
     * 修改报障单
     *
     * @param reportOrder 报障单
     * @return 结果
     */
    @Override
    public int updateReportOrder(ReportOrder reportOrder) {
        List<String> uploadFiles = reportOrder.getUploadFiles();
        reportOrder.setReportFiles(null);
        if (uploadFiles != null && !uploadFiles.isEmpty()) {
            reportOrder.setReportFiles(String.join(";", uploadFiles));
        }
        return reportOrderMapper.updateReportOrder(reportOrder);
    }

    /**
     * 批量删除报障单
     *
     * @param ids 需要删除的报障单主键
     * @return 结果
     */
    @Override
    public int deleteReportOrderByIds(Long[] ids) {
        return reportOrderMapper.deleteReportOrderByIds(ids);
    }

    /**
     * 删除报障单信息
     *
     * @param id 报障单主键
     * @return 结果
     */
    @Override
    public int deleteReportOrderById(Long id) {
        return reportOrderMapper.deleteReportOrderById(id);
    }
}
